//
//  networkManger.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/28.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "networkManger.h"

@implementation networkManger


/** Post 请求 */
+(void)PostHttpDataWithUrlStr:(NSString *)url Dic:(NSDictionary *)dic SuccessBlock:(SuccessBlock)successBlock FailureBlock:(FailedBlock)failureBlock
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];// 请求返回的格式为json
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",
                                                         @"application/json",
                                                         @"text/plain",
                                                         @"text/json",
                                                         @"image/jpeg",
                                                         @"text/javascript", nil];
    if ([self getCookieString])
    {
        [manager.requestSerializer setValue:[self getCookieString] forHTTPHeaderField:@"cookie"];
    }
//    UIWindow *V = [UIApplication sharedApplication].keyWindow;
//    MBProgressHUD *hud = [MBProgressHUD gk_toastShowTo:V message:@"加载中..."];
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        // 获取所有数据报头信息
        NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)task.response;
        NSString *cookieString = [[HTTPResponse allHeaderFields] valueForKey:@"Set-Cookie"];
        if (cookieString != nil) {
            [self saveCookies:cookieString];
        }
        /** 这里是处理事件的回调 */
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        /** 这里是处理事件的回调 */
        if (failureBlock) {
            failureBlock(error);
        }
    }
     ];
}

//合适的时机保存Cookie
+ (void)saveCookies:(NSString *)cookie {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:cookie forKey: @"userCookie"];
    [defaults synchronize];
}
+ (NSString *)getCookieString {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"userCookie"];
}
/** Get 请求 */
+(void)GetHttpDataWithUrlStr:(NSString *)url Dic:(NSDictionary *)dic SuccessBlock:(SuccessBlock)successBlock FailureBlock:(FailedBlock)failureBlock{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];// 请求返回的格式为json
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",
                                                         @"application/json",
                                                         @"text/plain",
                                                         @"text/json",
                                                         @"image/jpeg",
                                                         @"text/javascript", nil];
    
    [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        /** 这里是处理事件的回调 */
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        /** 这里是处理事件的回调 */
        if (failureBlock) {
            failureBlock(error);
        }
    }
     
     ];
    
}
//#pragma mark - 设置Header
//+ (instancetype)addHeader:(NSString *)headerString
//                 forField:(NSString *)fieldString {
//    [self.manager.requestSerializer setValue:headerString
//                          forHTTPHeaderField:fieldString];
//    return self;
//}
@end
